<!--
  咨询分类界面
-->
<template>
  <div>
    <!-- 按钮开始 -->
    <div>
      <el-button type="primary" size="small" @click="toAdd">添加</el-button>
    </div>
    <!-- 按钮结束 -->

    <!-- 表格开始 -->
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="id" label="编号"> </el-table-column>
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column prop="description" label="描述"> </el-table-column>
        <el-table-column prop="no" label="序号"> </el-table-column>
        <el-table-column prop="parentId" label="父资讯编号"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              @click="toEdit(scope.row)"
              type="success"
              size="small"
              plain
              >编辑</el-button
            >
            <el-button
              @click="toDelete(scope.row.id)"
              type="warning"
              size="small"
              plain
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 表格结束 -->

    <!-- 模态框开始-->
    <el-dialog :title="title" :visible.sync="visible">
      <el-form :model="form" :rules="rules" ref="myForm">
        <el-form-item
          label="咨询名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="咨询描述" :label-width="formLabelWidth">
          <el-input v-model="form.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="咨询序号" :label-width="formLabelWidth" prop="no">
          <el-input v-model="form.no" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="父资讯编号"
          :label-width="formLabelWidth"
          prop="parentId"
        >
          <el-input v-model="form.parentId" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="toSubmit('myForm')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>

<script>
import { findAll, saveOrUpdate, deleteById } from "@/api/classify";

export default {
  data() {
    return {
      form: {},
      tableData: [],
      title: "",
      visible: false,
      formLabelWidth: "100px",
      rules: {
        name: [{ required: true, message: "请输入咨询名称", trigger: blur }],
        no: [{ required: true, message: "请输入咨询序号", trigger: blur }],
        parentId: [
          { required: true, message: "请输入父咨询编号", trigger: blur },
        ],
      },
    };
  },
  methods: {
    //查询所有的咨询
    async findAllClassify() {
      let res = await findAll();
      // console.log(res);
      this.tableData = res.data;
    },
    //新增按钮绑定事件
    toAdd() {
      this.visible = true;
      this.title = "新增网站咨询分类";
      this.form = {};
    },
    //为编辑按钮绑定事件
    toEdit(row) {
      this.visible = true;
      this.title = "修改网站咨询分类";
      this.form = { ...row };
    },
    //为删除按钮绑定事件
    toDelete(id) {
      this.$confirm("此操作将永久删除该栏目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await deleteById({ id });
          this.findAllClassify();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //模态框确认按钮绑定事件
    toSubmit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await saveOrUpdate(this.form);
          this.visible = false;
          this.$message({
            message: res.message,
            type: "success",
          });
          // 刷新页面
          this.findAllClassify();
        } else {
          //校验失败的执行结果
          this.$message.error("保存失败");
          return false;
        }
      });
    },
  },
  created() {
    this.findAllClassify();
  },
};
</script>

<style lang="less" scoped>
</style>
